package com.movitech.mobile.cz.base.purchase.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.movitech.mobile.cz.base.common.aop.ServiceThrowable;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.purchase.entity.SAbutmentLogs;
import com.movitech.mobile.cz.base.purchase.mapper.SAbutmentLogsMapper;
import com.movitech.mobile.cz.base.purchase.service.SAbutmentLogsService;
import com.movitech.mobile.cz.base.util.UserUtils;

import org.springframework.stereotype.Service;

/**
 * <p>
 * 对接日志 服务实现类
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-06-07
 */
@Service("sAbutmentLogsService")
public class SAbutmentLogsServiceImpl extends ServiceImpl<SAbutmentLogsMapper, SAbutmentLogs> implements SAbutmentLogsService {

    @Override
    public int saveAbutmentLogs(Integer abutmentType, String relatedId,
                                String abutmentContent, Integer abutmentStatus, Integer currentAbutmentFlag) {

        SAbutmentLogs sAbutmentLogs = new SAbutmentLogs();
        sAbutmentLogs.setAbutmentType(abutmentType);
        sAbutmentLogs.setRelatedId(relatedId);
        sAbutmentLogs.setAbutmentContent(abutmentContent);
        sAbutmentLogs.setAbutmentStatus(abutmentStatus);
        sAbutmentLogs.setCurrentAbutmentFlag(currentAbutmentFlag);
        sAbutmentLogs.setBaseFeilds(UserUtils.getCurrentUserId(), null, CommonConstants.NO_INTEGER, true);
        return baseMapper.insert(sAbutmentLogs);
    }

    @Override
    public int saveAbutmentLogs(Integer abutmentType, String relatedId, String abutmentContent, String remarks, Integer abutmentStatus, Integer currentAbutmentFlag) {
        SAbutmentLogs sAbutmentLogs = new SAbutmentLogs();
        sAbutmentLogs.setAbutmentType(abutmentType);
        sAbutmentLogs.setRelatedId(relatedId);
        sAbutmentLogs.setAbutmentContent(abutmentContent);
        sAbutmentLogs.setAbutmentStatus(abutmentStatus);
        sAbutmentLogs.setRemarks(remarks);
        sAbutmentLogs.setCurrentAbutmentFlag(currentAbutmentFlag);
        sAbutmentLogs.setBaseFeilds(UserUtils.getCurrentUserId(), null, CommonConstants.NO_INTEGER, true);
        return baseMapper.insert(sAbutmentLogs);
    }

    @Override
    public int saveAbutmentLogs(SAbutmentLogs sAbutmentLogs) {
        sAbutmentLogs.setBaseFeilds(UserUtils.getCurrentUserId(), null, CommonConstants.NO_INTEGER, true);
        return baseMapper.insert(sAbutmentLogs);
    }

    @Override
    public int updateAbutmentLogs(Integer abutmentType, String relatedId,
                                  Integer abutmentStatus, Integer currentAbutmentFlag) throws RuntimeException {

        SAbutmentLogs param = new SAbutmentLogs();
        param.setAbutmentType(abutmentType);
        param.setRelatedId(relatedId);
        param.setCurrentAbutmentFlag(1);
        param.setDelFlag(CommonConstants.NO_INTEGER);
        SAbutmentLogs sAbutmentLogsDB = baseMapper.selectOne(param);

        if (sAbutmentLogsDB == null) {
            throw new RuntimeException("要更新的对接日志不存在", new ServiceThrowable());
        }

        sAbutmentLogsDB.setAbutmentStatus(abutmentStatus);
        sAbutmentLogsDB.setCurrentAbutmentFlag(currentAbutmentFlag);
        sAbutmentLogsDB.setBaseFeilds(UserUtils.getCurrentUserId(), null, CommonConstants.NO_INTEGER, false);

        return baseMapper.updateById(sAbutmentLogsDB);
    }

}
